package com.example.demo1;


import com.example.demo1.entity.Users;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
// 等同于以下三个注解
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan("com.example.demo1")
public class Demo1Application {

    public static void main(String[] args) {
        // 1、返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(Demo1Application.class, args);

        // 2、查看容器里面的组件
//        String[] names = run.getBeanDefinitionNames();
//        for (String name : names) {
//            System.out.println(name);
//        }

        // 3、从容器中获取组件
//        Pet tom1 = run.getBean("tom", Pet.class);
//        Pet tom2 = run.getBean("tom", Pet.class);
//        System.out.println("tom1 == tom2 ??? " + (tom1 == tom2)); // true

        // 4.
//        MyConfig bean = run.getBean(MyConfig.class);
//        System.out.println(bean); // com.example.demo1.config.MyConfig@64e9807c

        /*
        MyConfig bean = run.getBean(MyConfig.class);
        Users user1 = bean.user01();
        Users user2 = bean.user01();
        // 当 @Configuration 时，结果是true。说明是单例模式
        // 当 @Configuration(proxyBeanMethods = false) 时，结果是 false。说明非单例
        System.out.println(user1 == user2);
        */

        /*
        Users user01 = run.getBean("user01", Users.class);
        Pet tom = run.getBean("tom", Pet.class);
        // 当 @Configuration 时，结果是true。说明是单例模式
        // 当 @Configuration(proxyBeanMethods = false) 时，结果是 false。说明非单例
        System.out.println("user01 的宠物：" + (user01.getPet() == tom));
         */

        // 5、获取组件
//        String[] beanNamesForType = run.getBeanNamesForType(Users.class);
//
//        for (String str : beanNamesForType) {
//            System.out.println(str); // user01
//        }

//        DBHelper bean = run.getBean(DBHelper.class);
//        System.out.println(bean);


        /**
         * 如果注释 @Bean("tom22")，调用 @Bean("tom")，打印的结果就是 true true fasle
         */
//        boolean tom = run.containsBean("tom");
//        System.out.println("容器中Tom组件：" + tom); // false
//
//        boolean user01 = run.containsBean("user01");
//        System.out.println("容器中user01组件：" + user01); // true
//
//        boolean tom22 = run.containsBean("tom22");
//        System.out.println("容器中tom22组件：" + tom22); // true

        /*
            公司使用bean.xml文件生成配置bean，然而你为了省事，想继续复用bean.xml，@ImportResource粉墨登场。
            @ImportResource("classpath:config/bean.xml")
            public class MyConfig {
                ...
            }
         */
        boolean boss = run.containsBean("boss");
        boolean cat = run.containsBean("cat");
        System.out.println("是大佬吗？" + boss); // true
        System.out.println("是大佬的宠物吗？" + cat); // true
    }

}
